﻿using PublSystem.Models;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PublSystem
{
    public partial class Search : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string q = "";
            string a = "";
            int p = 1;
            try
            {
                q = Request.QueryString["q"];
                if (q != null && q != "")
                {
                    this.Page.Title = "Search by content: " + q;
                    textSearchByContent.Text = q;
                }
            }
            catch
            {
                q = "";
            }

            try
            {
                a = Request.QueryString["a"];
                if (a != null && a != "")
                {
                    this.Page.Title = "Search by author: " + a;
                    textSearchByAuthor.Text = a;
                }
            }
            catch
            {
                a = "";
            }

            if (q != "" && q != null)
                using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString()))
                {
                    SqlDataAdapter cmd = new SqlDataAdapter("SELECT TOP 30 * FROM PublicationsInfo.Publications WHERE title LIKE '%" + q + "%' OR summary LIKE '%" + q + "%' AND PublicationsInfo.Publications.publicationId < " + p * 10 + "AND PublicationsInfo.Publications.publicationId > " + (p - 1) * 10 + "  ORDER BY PublicationsInfo.Publications.publicationId", conn);
                    conn.Open();
                    DataSet ds = new DataSet();
                    cmd.Fill(ds);

                    publs.DataSource = ds;
                    publs.DataBind();
                }

            if (a != "" && a != null)
                using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString()))
                {
                    conn.Open();
                    SqlDataAdapter cmd = new SqlDataAdapter(@"SELECT TOP 30 * FROM PublicationsInfo.Publications
                                                        , PublicationsInfo.AuthorsPublications , PublicationsInfo.Authors WHERE PublicationsInfo.AuthorsPublications.authorId = PublicationsInfo.Authors.id 
                                                        AND PublicationsInfo.Publications.publicationId = PublicationsInfo.AuthorsPublications.publicationId
                                                        AND PublicationsInfo.Authors.name LIKE '%" + a + "%'", conn);

                  
                    DataSet ds = new DataSet();
                    cmd.Fill(ds);

                    publs.DataSource = ds;
                    publs.DataBind();
                }
        }

        protected void buttonSearchByAuthor_Click(object sender, EventArgs e)
        {
            Response.Redirect("Search?a=" + textSearchByAuthor.Text);
        }

        protected void buttonSearchSearchByContent_Click(object sender, EventArgs e)
        {
            Response.Redirect("Search?q=" + textSearchByContent.Text);
        }

        protected void buttonSearchByKeyword_Click(object sender, EventArgs e)
        {
            Response.Redirect("Keyword?q=" + textSearchByKeyword.Text);
        }
    }
}